package com.information.manage.base.sevice.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.information.manage.base.mapper.GongquMapper;
import com.information.manage.base.mapper.ZhanquMapper;
import com.information.manage.base.model.entity.Gongqu;
import com.information.manage.base.model.entity.Zhanqu;
import com.information.manage.base.model.query.PageQuery;
import com.information.manage.base.sevice.GongquService;
import com.information.manage.base.sevice.ZhanquService;
import com.information.manage.basic.common.result.Result;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;

@Service
public class GongquServiceImpl extends ServiceImpl<GongquMapper, Gongqu> implements GongquService {

    @Resource
    private GongquMapper gongquMapper;
    @Resource
    private ZhanquMapper zhanquMapper;


    @Override
    public Page<Gongqu> getgongquByPage(PageQuery pageQuery) {
        Page<Gongqu> page = new Page<>(pageQuery.getCurPage(), pageQuery.getPageSize());
        if(pageQuery.getName()!=null) {
            LambdaQueryWrapper<Gongqu> wrapper = new LambdaQueryWrapper<>();
            wrapper.like(Gongqu::getGongquname, pageQuery.getName());
            return gongquMapper.selectPage(page, wrapper);
        }
        return gongquMapper.selectPage(page,null);
    }

    @Override
    public Result<Integer> updateGongqu(Gongqu gongqu) {
        LambdaQueryWrapper<Zhanqu> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Zhanqu::getGqId,gongqu.getGongquid());
        if(zhanquMapper.selectList(queryWrapper).isEmpty()){
            return Result.ok(gongquMapper.updateById(gongqu));
        }
        return Result.fail("此工区已经被绑定，不能修改，请联系管理人员");
    }
}
